package ModelLayer;

import java.util.ArrayList;
/**
 * Write a description of class AddressBook here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class AddressBook
{
    // instance variables - replace the example below with your own
    private ArrayList<Person> p;

    /**
     * Constructor for objects of class AddressBook
     */
    public AddressBook()
    {
        // initialise instance variables
        p = new ArrayList<Person>();
    }

    /**
     * this method adds persons p to the arraylist
     * @param  Person p
     */
    public void addPerson(Person p)
    {
        this.p.add(p);
    }

    /**
     * this method grants access to the persons name
     * @param   name
     * @return  p(Person object)
     */
    public Person readPerson(String name)
    {
        Person p = findPerson(name);
        return p;
    }

    /**
     * this method removes a person object p from the arraylist
     * @param   Person p
     */
    public void removePerson(Person p)
    {
        this.p.remove(p);
    }

    /**
     * makes it possible to update persons
     * @param ID, name, address, postalCode, town, phoneNo
     */
    public void updatePerson(int ID, String name, String address, String postalCode, String town, int phoneNo)
    {
        findPerson(name).updatePerson(ID, name, address, postalCode, town, phoneNo);
    }

    /**
     * this methods makes it possible to search for a person
     * @param name
     * @return person object p
     */
    public Person findPerson(String name)
    {
        int index = 0;
        while(p.size() > index)
        {
            Person persons = p.get(index);
            if(persons.getName().contains(name))
            {
                return persons;
            }
            index++;
        }
        return null;
    }

    /**
     * this method returns the whole arraylist
     */
    public ArrayList<Person> listAllPersons()
    {
        return p;
    }

}
